package com.itheima.studentSystem;

import com.itheima.studentSystem.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

/*
     学生管理系统
     重点:1.界面展示
            -----欢迎使用学生管理系统-----
            1.添加学生
            2.删除学生
            3.修改学生
            4.查看学生
            5.退出
          2.防止重复出现
 */
public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> students = new ArrayList<>();//建立学生列表存储数据

        //1.建立界面展示
        lo:
        while (true) {
            System.out.println("-----欢迎使用学生管理系统-----");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            int choice = sc.nextInt();

            //1.1选择建立
            switch (choice) {
                case 1:
                    addStudent(students);
                    break;
                case 2:
                    delStudent(students);
                    break;
                case 3:
                    updateStudent(students);
                    break;
                case 4:
                    checkStudent(students);
                    break;
                case 5:
                    System.out.println("感谢您的使用!");
                    break lo;
                default:
                    System.out.println("您的输入有误,请重新输入!");
                    break;
            }
        }
    }

    //2.排同算法
    public static int getIndex(ArrayList<Student> students, String sid) {
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            Student stu = students.get(i);
            if (sid.equals(stu.getSid())) {
                index = i;
            }
        }
        return index;
    }

    //添加学生
    public static void addStudent(ArrayList<Student> students) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号:");
        String sid = sc.next();
        int index = getIndex(students, sid);
        if (index != -1) {
            System.out.println("该用户已存在!");
            return;
        }
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入生日:");
        String birthday = sc.next();
        Student stu = new Student(sid, name, age, birthday);
        students.add(stu);
        System.out.println("添加成功!");
    }

    //删除学生
    public static void delStudent(ArrayList<Student> students) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号:");
        String sid = sc.next();
        int index = getIndex(students, sid);
        if (index == -1) {
            System.out.println("该用户不存在!");
            return;
        }
        students.remove(index);
        System.out.println("删除成功!");
    }


    //修改学生
    public static void updateStudent(ArrayList<Student> students) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号:");
        String sid = sc.next();
        int index = getIndex(students, sid);
        if (index == -1) {
            System.out.println("该学生不存在!");
            return;
        }

        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入生日:");
        String birthday = sc.next();
        Student stu = new Student(sid, name, age, birthday);
        students.set(index, stu);
    }

    //查看学生
    public static void checkStudent(ArrayList<Student> students) {
        if (students.size() == 0) {
            System.out.println("暂未添加学生!");
            return;
        }
        System.out.println("学号\t\t" + "姓名\t" + "年龄\t\t" + "生日");
        for (int i = 0; i < students.size(); i++) {
            Student stu = students.get(i);
            System.out.println(stu.getSid() + "\t\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
        }
    }
}
